package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import androidx.appcompat.widget.AppCompatRadioButton;
import android.view.View;
import android.widget.CompoundButton;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.sgb.kjwl.view.widget.TagTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class UserAddressAdapter extends BaseRecycleAdapter<UserAddressEntity> {

    private onEditClickListener editClickListener;
    private AppCompatRadioButton appCompatRadioButton;

    public void setonEditClick(onEditClickListener editClickListener) {
        this.editClickListener = editClickListener;
    }

    public interface onEditClickListener {
        void onEditClick(UserAddressEntity data);

        void onDelClick(String id);

        void isCheck(boolean isCheck,int position);
    }


    public UserAddressAdapter(Context context, List<UserAddressEntity> datas) {
        super(context, R.layout.item_address_user, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, UserAddressEntity data) {
        holder.setBinding(BR.viewModel, data);
        TagTextView tvDetail = holder.itemView.findViewById(R.id.tv_detail);
        appCompatRadioButton = holder.itemView.findViewById(R.id.radio_is_def);
        List<String> tags = new ArrayList<>();
        if (data.isDefaultAdress()) {
            tags.add("默认");
            appCompatRadioButton.setChecked(true);
            appCompatRadioButton.setText("已设为默认");
        } else {
            appCompatRadioButton.setChecked(false);
            appCompatRadioButton.setText("设为默认");
        }
        tvDetail.setContentAndTag(data.getAddress(), tags);


        holder.itemView.findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onEditClick(data);
            }
        });

        holder.itemView.findViewById(R.id.tv_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onDelClick(data.getId());
            }
        });

        appCompatRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b) {
                    return;
                }
                editClickListener.isCheck(b,holder.getPosition()-1);
            }
        });
    }
}
